﻿<!DOCTYPE html>
@{
    LayoutPage ="_Layout.cshtml";

    string name="";
    string email="";
    string company="";
    string title="";
    string timezone="";
    string location="";
    string optin="";
    
    var db = Database.OpenFile("MvcConf.sdf");
    if(IsPost){
        name = Request["name"];
        email = Request["email"];
        company = Request["company"];
        title = Request["title"];
        timezone = Request["timezone"];
        location = Request["location"];
        optin = Request["optin"];
        
        
        if(name.IsEmpty())
        {
            Validation.AddFieldError("name","Name is required.");
        }
        if(email.IsEmpty())
        {
            Validation.AddFieldError("email","Email is required.");
        }else if(!email.IsEmail())
        {
            Validation.AddFieldError("email","Email is not a valid email.");
        }
        
        if(timezone.IsEmpty())
        {
            Validation.AddFieldError("timezone","Timezone is required.");
        }
        if(optin.IsFalse()){
            Validation.AddFieldError("optin","You must agree to opt in or we cannot send you information about the conference.");
        }
        if(Validation.Success){
            var insert = "INSERT INTO Attendee1 (Name,Email,Company,Title,Timezone,Location) VALUES(@0,@1,@2,@3,@4,@5)";
            db.Execute(insert,name,email,company,title,timezone,location);
            Response.Redirect("~/attend-thankyou.cshtml");
        }
    }
    else{
        var count = db.QueryValue("select count(id) as count from attendee1");
        if(count>=2000)
        {
            Response.Redirect("/RegistrationClosed");
        }
        
    }
}
<h2>Register</h2>
    <div class="errorMessage  ui-state-highlight">
    @Html.ValidationSummary("Please correct the following fields:")
    </div>
<form method="POST">
<fieldset class="form">
<p class="clear"><label for="Name">Name:</label><input type="text" name="Name" value="@name"></p><br/>
<p class="clear"><label for="Email">Email:</label><input type="text" name="email" id="email" value="@email"></p><br/>
<p class="clear"><label for="Company">Company:</label><input type="text" name="Company" value="@company"></p><br/>
<p class="clear"><label for="Title">Title:</label><input type="text" name="Title" value="@title"></p><br/>
<p class="clear"><label for="Timezone">Timezone:</label><input type="text" name="timezone" value="@timezone"></p><br/>
<p class="clear"><label for="Location">Location:</label><input type="text" name="location" value="@location"></p><br/>
<p class="clear"><label for="optin">Opt In:</label><input type="checkbox" value="true" name="optin">Allow Mvc Conf to contact you via email.</p><br/>
<p class="clear"><label for"submit">&nbsp;</label><input class="button-small" type="submit" value="Submit" /></p><br/>
</fieldset>
</form>